import dayjs from "../../libs/dayjs"
import { AstContext } from "../context"
import { useRoot } from "../utils/useRoot"
import { useSetting } from "../utils/useSetting"
import { visit } from "../visitor"
import { closeRoomVisitor } from "./closeRoomVisitor"
import { RedPacketInfo } from "./redPacketInfoVisitor"
import { redPacketListInfoVisitor } from "./redPacketListInfoVisitor"
import { roomInfoVisitor } from "./roomInfoVisitor"
import { getShareLink } from "./shareLinkVisitor"
export interface RoomInfo {
    openTime: string;
    status: string;
    online: number;
    redPacketList: RedPacketInfo[];
    name: string;
}
export function getRoomInfo(start: boolean): AstContext {
    const ctx = new AstContext(dayjs)
    let node = selector().findOnce()
    while (!node) {
        sleep(1000)
    }
    node.recycle()
    const statusText = id("live_red_packet_status_text").findOne(1000 * 5)
    if (statusText) {
        // 有红包
        const text = statusText.text()
        statusText.recycle()
        if (text !== '进行中') {
            const setting = useSetting()
            const roomNode = useRoot()
            visit(roomNode, roomInfoVisitor, ctx)
            // 检查openTime是否满足条件
            const openTime = ctx.get<string>('openTime')
            if (openTime) {
                const openDate = dayjs(openTime, 'YYYY-MM-DD HH:mm:ss').toDate()
                if (openDate > new Date(new Date().getTime() + 30 * 1000)) {
                    // 距离开奖大于30秒
                    if (setting.isTakeRedInfo) {
                        const openRedPacket = ctx.get<Function>('openRedPacket')
                        if (openRedPacket) {
                            console.log(`找到了打开红包按钮`)
                            openRedPacket()
                            roomNode.recycle()
                            sleep(1000)
                            // 查找
                            const r = useRoot()
                            r.recycle()
                            sleep(1000)
                            // 关闭
                            let closeRedPacket = ctx.get<Function>('closeRedPacket')
                            while (!closeRedPacket) {
                                const root = useRoot()
                                visit(root, redPacketListInfoVisitor, ctx)
                                closeRedPacket = ctx.get<Function>('closeRedPacket')
                            }
                            if (closeRedPacket) {
                                console.log(`找到了关闭红包按钮`)
                                closeRedPacket()
                            }
                        } else {
                            roomNode.recycle()
                        }
                    } else {
                        roomNode.recycle()
                    }
                    const redPacketList = ctx.get<RedPacketInfo[]>('redPacketList')
                    if (setting.type && redPacketList && redPacketList.length) {
                        const types = setting.type
                        const hasRed = redPacketList.some(it => types.includes(it.type))
                        if (hasRed) {
                            // 获取分享链接
                            console.log("获取分享链接")
                            const link = getShareLink(start)
                            if (link) {
                                ctx.set('share', link.all())
                            } else {
                                console.log("未找到分享链接")
                            }
                        }
                    }

                } else {
                    roomNode.recycle()
                }
            }
        }
    }
    sleep(1000 * 5)
    // 退出直播间
    ctx.set('closeRoom', undefined)
    let closeRoom = ctx.get<Function>("closeRoom")
    console.log(`close room is ${closeRoom ? true : false}`)
    let count = 2;
    while (!closeRoom && count > 0) {
        const root = useRoot()
        console.log(`获取退出房间按钮`)
        visit(root, roomInfoVisitor, ctx)
        closeRoom = ctx.get<Function>("closeRoom")
        if (!closeRoom) {
            console.log(`未找到退出房间按钮`)
        }
        console.log(`close room is ${closeRoom ? true : false}`)
        root.recycle()
        sleep(1000)
        count -= 1;
    }
    if (closeRoom) {
        closeRoom()
    } else {
        back()
    }
    sleep(2000)
    const root = useRoot()
    visit(root, closeRoomVisitor, ctx)
    const sureCloseRoom = ctx.get<Function>("sureCloseRoom")
    if (sureCloseRoom) {
        sureCloseRoom()
    }
    return ctx;
}
